<?php
namespace Home\Service;

/*use Cli\Service\CustomerCountServiceModel;*/
/*use Home\Model\DailyDataModel;*/
/*use Home\Model\GroupCustomerStatisticsModel;
use Home\Model\UserCustomerStatisticsModel;
use Common\Lib\GetWeek;
use Common\Lib\GetMonth;*/

class DailyDataGather {

    private $start =   '';
    private $end   =   '';
    private $order = 'id asc';

    private $today = '';


    public function __construct(){
        $this->today = Date('Y-m-d');
    }



    private function getFields(){
        return array('collection_num',  'download_num', 'phone_num', 'invitation_num', 'actual_num', 'preliminary_pass', 'self_employment', 'introduce_num');
    }


    private function getSqlFields(){
        $field = $this->getFields();
        $arr = array();
        foreach ($field as $key => $value) {
            $arr[] = "sum(`". $value . "`) as ".$value;
        }
        return implode(",", $arr);
    }
    public function reSort($re){
        $columen = array_column($re, $this->field);
        array_multisort($columen, $this->sort , SORT_NUMERIC, $re);
        return $re;
    }

/*    public function getDepartment($start,$end,$department_id,$role_id,$sort_field,$sort_order){
        $this->start =$start;
        $this->end = $end;
         $department_id = $department_id;
        $role_id = $role_id;
        switch($role_id){
            case 16:
              $list  = M('daily_data')->field($this->getSqlFields().", department_id as id, department_name as name")
                                                            ->where(array('date'=> array(array('EGT',$this->start),array('ELT',$this->end)),'department_id'=>$department_id))
                                                            ->order($sort_field.' '.$sort_order)
                                                            ->select(); 
            break;
            default :
              $list  = M('daily_data')->field($this->getSqlFields().", department_id as id, department_name as name")
                                                            ->where(array('date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->group('department_id')
                                                            ->order($sort_field.' '.$sort_order)
                                                            ->select();
              
        }                                                                                  
        return $this->reSort($list);
    }*/
    /*重新更改 开始*/
       public function setDate($start, $end){
        $this->start = $start;
        $this->end   = $end;
        return $this;
    }
     public function setOrder($order){
        $this->order = $order;
        $fields  = explode(' ', $this->order);
        $this->field = $fields[0];
        $sortMap = array('asc'=>SORT_ASC, 'desc'=>SORT_DESC );
        $this->sort = $sortMap[$fields[1]];
        return $this;
    }
       public function getDepartment(){
            /*     if ($this->end >=  $this->today ) {

                    $toDaylist =  $this->getTodayDepartment();
                } else {
                    $toDaylist = array();
                }*/
                   
            //不使用接口
              $list  = M('daily_data')->field($this->getSqlFields().", department_id as id, department_name as name")
                                                            ->where(array('date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                            ->group('department_id')
                                                            ->select();
            //使用接口
        /*    $start = $this->start;
            $end = $this->end;
            $url = "Department/getSatisticDepartment?p=1";
            if($start!=""&&$end!=""){
                $url=$url."&start=".$start."&end=".$end;
            }
            $result = http_get($url);
            $result = json_decode($result,true);
            $list = $result["list"];*/
    
         
        //$list  = $this->mergeList($list2, $toDaylist);
        return $this->reSort($list);
    }


       public function getUsers($department_id){
      /*  if ($this->end >=  $this->today) {
            $toDaylist = $this->getTodayUsers($group_id); 
        } else {
            $toDaylist = array();   
        }*/
      
      //不使用接口
        $list = M('daily_data')->join("user_info as ui on daily_data.user_id=ui.user_id")
                                                ->field($this->getSqlFields().", daily_data.user_id as id, realname as name")
                                                ->where(array('daily_data.department_id'=>$department_id ))
                                                ->where(array('date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                ->group('daily_data.user_id')
                                                ->select();
        //使用接口
       /*      $start = $this->start;
            $end = $this->end;
           $url = "Department/getUsersDailyData?p=1";
            if(is_numeric($department_id)&&$department_id!=0){
                $url=$url."&department_id=".$department_id;
            }
            if($start!=""&&$end!=""){
                $url=$url."&start=".$start."&end=".$end;
            }
            $result = http_get($url);
            $result = json_decode($result,true);
            $list = $result["list"];*/
        
      
        //$list  = $this->mergeList($list2, $toDaylist);
        return $this->reSort($list);
    }


        public function getSingleUserData($user_id){
        $sources = array("1"=>"collection_num","2"=>"download_num","3"=>"phone_num","4"=>"invitation_num","5"=>"actual_num","6"=>"preliminary_pass","7"=>"self_employment","8"=>"introduce_num");
      
        $list = M('daily_data')->join("user_info as ui on daily_data.user_id=ui.user_id")
                                                ->field($this->getSqlFields().", daily_data.user_id as id, realname as name")
                                                ->where(array('daily_data.user_id'=>$user_id ))
                                                ->where(array('date'=> array(array('EGT',$this->start),array('ELT',$this->end))))
                                                //->group('daily_data.user_id')
                                                ->select();
        //$list  = $this->mergeList($list2, $toDaylist);
        $list = $this->dataEmpty($list,$sources);
        //$list['0']['ceshi'] = 0;
        return $this->reSort($list);
    }
    //将空数据处理成零
    private function dataEmpty($list,$sources){
        foreach($sources as $key =>$value){
            foreach($list as $k =>$v){
                if(empty($v[$value])){
                    $list[$k][$value] = 0;
                }
            } 
        }
         return $list; 
    }

    private function mergeList($list, $list2){
        $list2 = arr_to_map($list2, 'id');
         // is_numeric
        foreach ($list as $key => $value) {
            if (isset($list2[$value['id']])) {
                foreach ($value as $field => $val) {
                    if ($field != 'id' && is_numeric($val)) {
                        $list[$key][$field] += $list2[$value['id']][$field];
                    }
                }   
            }
        }

        $list = arr_to_map($list, 'id');
        $delt = array_diff_key($list2, $list);
        $list = array_values($list);
        if ($delt) {
           $list = array_merge($list, $delt); 
        }
        return $list;
    }

    /*重新更改 结束*/




    
}